package com.jy.adscm.operation.mscyb.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.mscyb.bean.JqcsSettingsSop;
import com.jy.adscm.operation.mscyb.service.JqcsSettingsSopService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.zltx.bean.SjFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-06-12
 */
@RestController
@RequestMapping("/adscm/operation/jqcs/jqcsSettingsSop")
public class JqcsSettingsSopController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    JqcsSettingsSopService jqcsSettingsSopService;

    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody List<JqcsSettingsSop> jqcsSettingsSop, HttpServletRequest request){
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        //删除之前的数据
        if (jqcsSettingsSop!=null&&jqcsSettingsSop.size()>0){
            JqcsSettingsSop jqbzSettingTech = jqcsSettingsSop.get(0);
            Wrapper<JqcsSettingsSop> wrapper =new EntityWrapper<>();
            wrapper.eq("pid",jqbzSettingTech.getPid());
            List<JqcsSettingsSop> oldJqcsSettingsSop = jqcsSettingsSopService.selectList(wrapper);
            for (JqcsSettingsSop oldJqbzSettingTech : oldJqcsSettingsSop) {
                jqcsSettingsSopService.deleteById(oldJqbzSettingTech.getId());
            }
            //新增
            for (JqcsSettingsSop settingTech : jqcsSettingsSop) {
                jqcsSettingsSopService.insert(settingTech);
            }
        }
        return  new SimpleCoreResponse("200","新增成功",null);
    }

    @RequestMapping("selectBybid/{pid}")
    public  SimpleCoreResponse  selectBybid(@PathVariable String pid) throws AdscmSystemException {
        List<SjFile>   sjFiles = jqcsSettingsSopService.selectJqcsSettingsSopByPid(pid);
        return  new SimpleCoreResponse("200","查询成功",sjFiles);
    }
    @RequestMapping("selectAll")
    public  SimpleCoreResponse  selectAll() throws  AdscmSystemException{
        List<SjFile>   sjFiles = jqcsSettingsSopService.selectAll();
        return  new SimpleCoreResponse("200","查询成功",sjFiles);
    }
}

